import 'package:flutter/material.dart';

import 'ink_widget.dart';

/// todo: 下拉筛选组件
class DropDownButton extends StatefulWidget {
  final List<DropDownButtonModel> data;
  final Function(DropDownButtonModel)? onTap;
  final String? hin;
  const DropDownButton({Key? key, this.data: const [], this.onTap, this.hin}) : super(key: key);

  @override
  _DropDownButtonState createState() => _DropDownButtonState();
}

class _DropDownButtonState extends State<DropDownButton> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return widget.data.length != 0 ?_dropDownSelect():Container();
  }

  _dropDownSelect() {
    return DropdownButton(
      items: [
        ...List.generate(widget.data.length, (index){
          return DropdownMenuItem(
            child: Center(
              child: InkWidget.text("${widget.data[index].name}",),
            ),
            value: widget.data[index].value ?? 0,
            onTap: (){
              if(widget.onTap != null) widget.onTap!(widget.data[index]);
            },
          );
        })
      ],
      hint: InkWidget.text(widget.hin ?? "",color: Colors.grey),
      underline: SizedBox(),
      elevation: 0,
      isDense: true,
      iconEnabledColor: Colors.grey,
      dropdownColor: Colors.grey.shade200,
      onChanged: (v){},
    );
  }
}

class DropDownButtonModel{
  String? name;
  dynamic value;
  int? index;
  DropDownButtonModel({this.value,this.index,this.name});
}